# coding: utf-8
from PyQt6.QtGui import QColor
from qfluentwidgets import FluentIcon as FIF, PrimaryPushButton, CompactSpinBox, ColorPickerButton, DoubleSpinBox, \
    ComboBox, InfoBar
from common import SVGFormat, SVGConverter, FileTypeIcon
from components import BaseScrollArea, DragDropButton, FileGroupCardWidget, FolderLineEdit, \
    OptimizeGroupHeaderCardWidget


class SVGConverterView(BaseScrollArea):
    title = 'SVG格式转换'
    description = f'SVG格式转换工具, 支持{SVGFormat.__args__}格式转换'
    object_name = 'SVGConverterView'
    icon = FileTypeIcon.SVG

    def __init__(self, parent=None):
        super().__init__(parent)
        self.addBtn = DragDropButton(self)
        self.fileGroupCard = FileGroupCardWidget('SVG文件', self)
        self.groupHeaderCard = OptimizeGroupHeaderCardWidget('转换设置', self)
        self.savePathLineEdit = FolderLineEdit(self)
        self.outputHeightSpinBox = CompactSpinBox(self)
        self.outputWidthSpinBox = CompactSpinBox(self)
        self.colorPickerBtn = ColorPickerButton(QColor('#FFFFFF'), '背景颜色', self)
        self.dpiSpinBox = CompactSpinBox(self)
        # self.scaleSpinBox = DoubleSpinBox(self)
        self.formatComboBox = ComboBox(self)
        self.startBtn = PrimaryPushButton(FIF.PLAY, '开始转换', self)

        self.__initWidgets()
        self.__initLayouts()
        self.__initConnections()

    def __initWidgets(self):
        self.setReturnVisible(True)
        self.addBtn.setFilter(['svg', 'svgz'])

        self.outputWidthSpinBox.setRange(0, 100000000)
        self.outputHeightSpinBox.setRange(0, 100000000)
        self.dpiSpinBox.setRange(1, 100000000)
        self.dpiSpinBox.setValue(96)
        # self.scaleSpinBox.setRange(0.1, 100000000)
        # self.scaleSpinBox.setValue(1)

        self.formatComboBox.addItems(SVGFormat.__args__)

    def __initLayouts(self):
        self.groupHeaderCard.addGroup(FIF.FOLDER, '保存路径', '', self.savePathLineEdit, 1)
        self.groupHeaderCard.addGroup(FIF.ARROW_DOWN, '输出高度', '', self.outputHeightSpinBox, 0)
        self.groupHeaderCard.addGroup(FIF.REMOVE_FROM, '输出宽度', '', self.outputWidthSpinBox, 0)
        self.groupHeaderCard.addGroup(FIF.PALETTE, '背景颜色', '', self.colorPickerBtn, 0)
        self.groupHeaderCard.addGroup(FIF.FIT_PAGE, 'DPI', '', self.dpiSpinBox, 0)
        # self.groupHeaderCard.addGroup(FIF.BACK_TO_WINDOW, '缩放比例', '', self.scaleSpinBox, 0)
        self.groupHeaderCard.addGroup(FIF.APPLICATION, '输出格式', '', self.formatComboBox, 0)
        self.groupHeaderCard.addGroup(FIF.PLAY_SOLID, '开始转换', '', self.startBtn, 0)

        self.scrollLayout.addWidget(self.addBtn)
        self.scrollLayout.addWidget(self.fileGroupCard)
        self.scrollLayout.addWidget(self.groupHeaderCard)

    def __initConnections(self):
        self.addBtn.fileChanged.connect(self.fileGroupCard.addFile)
        self.startBtn.clicked.connect(self.onStarted)

    def onStarted(self):
        files = self.fileGroupCard.files
        save_path = self.savePathLineEdit.text()
        output_height = self.outputHeightSpinBox.value()
        output_width = self.outputWidthSpinBox.value()

        if output_height == 0:
            output_height = None
        if output_width == 0:
            output_width = None

        bg = self.colorPickerBtn.color.name()
        dpi = self.dpiSpinBox.value()
        scale = 1
        format = self.formatComboBox.currentText()

        if not files:
            InfoBar.warning('', '请选择文件', parent=self)
            return
        elif not save_path:
            InfoBar.warning('', '请选择保存路径', parent=self)
            return
        self.load(SVGConverter.pool_convert, files, save_path, output_width, output_height, bg, dpi, scale, format)
